package com.cloudtasks;
import com.cloudtasks.R;
import android.app.Dialog;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * The Custom Dialog class contains the constructor for creating an Edit or Add
 * dialog. Adding or Editing can be done on users, grocery lists, or single
 * items.
 * 
 * If Editing, you can: -Remove an object -Rename an object (also contains an
 * EditText for text input) -Cancel
 * 
 * If Adding, you can: -Add new object (also contains an EditText for text
 * input) -Cancel
 * 
 * @author Felix Wu
 */
public class CustomDialog extends Dialog {

	Context currCtx; // current context in which to display dialog
	String type;	// either "edit" or "add", dictates which dialog to show
	String target;	// what data to perform operation on: user, list, or item
	Database db;	// database handle to perform operation on
	Cursor cursor;	// cursor to requery and refresh after change has been made
	long objectID;	// ID of object to do operation on (could be user, list, or item)
	String itemType;	// only relevant when adding item, need to know type

	public CustomDialog(Context context, Database mdb, Cursor cursor,
			String type, String target) {
		super(context);
		this.currCtx = context;
		this.type = type;
		this.target = target;
		this.db = mdb;
		this.cursor = cursor;
	}
	// needed when renaming or removing, to specify which object to do operation on
	public void passNameID(long id) {
		this.objectID = id;
	}
	// needed if adding item, need to know type which item belongs to
	public void passItemType(String itemType) {
		this.itemType = itemType;
	}
	
	/* function that gets called when dialog is constructed */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// EDIT POPUP (RENAME AND REMOVE OBJECT)
		if (type.equals("edit")) {
			// only create the dialog and buttons if editing
			setContentView(R.layout.popup_edit);

			// create Remove button and Remove.OnClickListener
			Button button = (Button) findViewById(R.id.remove_button);
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					boolean success = false;
					if (target.equals("user")) {
						// remove a user
						success = db.deleteUser(objectID);
						cursor.requery(); // refresh list
					} else if (target.equals("list")) {
						// remove a grocery list
						success = db.deleteList(objectID);
						cursor.requery(); // refresh list
					} else if (target.equals("item")) {
						// remove a grocery item
						success = db.deleteItem(objectID);
						cursor.requery(); // refresh list
					}

					// indicate whether remove operation succeeded
					if (success == false)
						MyTools.displayToastMessage(currCtx,
								"Error: Did not remove");
					else
						MyTools.displayToastMessage(currCtx,
								"Successfully Removed");
					dismiss(); // dismiss dialog and return to original view
				}
			});
			// create Rename button and Rename.OnClickListener
			Button button1 = (Button) findViewById(R.id.rename_button);
			button1.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					boolean success = false;
					String newName = "";
					try {
						EditText input = (EditText) findViewById(R.id.new_name);
						newName = input.getText().toString();
						if(newName.equals("")){
							MyTools.displayToastMessage(currCtx,
									"Please use valid name");
						}
						else{
							dismiss(); // dismiss dialog and return to original view
							if (target.equals("user")) {
								success = db.updateUser(objectID, newName);
								cursor.requery();
							} else if (target.equals("list")) {
								success = db.updateList(objectID, newName);
								cursor.requery();
							} else if (target.equals("item")) {
								success = db.updateItemName(objectID, newName);
								cursor.requery();
							}
							// indicate whether renaming operation succeeded
							if (success == false)
								MyTools.displayToastMessage(currCtx,
										"Error: Did not rename");
							else
								MyTools.displayToastMessage(currCtx,
										"Successfully Renamed to " + newName);
						}
					} catch (Exception e) {
						MyTools.displayToastMessage(currCtx, e.toString());
					}
				}
			});
			// create Cancel button and Cancel.OnClickListener
			Button button2 = (Button) findViewById(R.id.cancel_button);
			button2.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss(); // dismiss dialog and return to original view
				}
			});
		}
		// ADD POPUP
		else if (type.equals("add")) {
			// only create the dialog and buttons if adding
			setContentView(R.layout.popup_add);

			// create Add button and Add.OnClickListener
			Button button = (Button) findViewById(R.id.confirm_add_button);
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					long success = -1;
					EditText input = (EditText) findViewById(R.id.new_name);
					String newName = input.getText().toString();
					if(newName.equals("")){
						MyTools.displayToastMessage(currCtx,
								"Please use valid name");
					}
					else{
						dismiss();
						if (target.equals("user")) {
							success = db.createUser(newName);
							cursor.requery();
						} else if (target.equals("list")) {
							success = db.createList(newName, objectID);
							cursor.requery();
						} else if (target.equals("type")) {
							success = db.createItem(newName, objectID, false,
									itemType);
							// cursor.requery();
						}
						// indicate whether adding operation succeeded
						if (success == -1)
							MyTools.displayToastMessage(currCtx,
									"Error: Did not add");
						else
							MyTools.displayToastMessage(currCtx,
									"Successfully Added " + newName);
					}
				}
			});
			// create Cancel button and Cancel.OnClickListener
			Button button1 = (Button) findViewById(R.id.cancel_add_button);
			button1.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss(); // dismiss dialog and return to original view
				}
			});
		}
	}

}